﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using Microsoft.EntityFrameworkCore;
using NetTaste;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TWarehouseRepository : BaseRepository<TWarehouse>, ITWarehouseRepository
    {
        public TWarehouseRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }


        /// <summary>
        /// 添加库房
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public bool AddWarehouse(TWarehouse m,out string message)
        {
            //string code = "Warehouse";
            //var aobj = Context.TWarehouses.AsNoTracking().Where(x => x.Code.Contains(code)).OrderByDescending(x => x.CreateDate).FirstOrDefault();
            //if (aobj != null)
            //{
            //    string pattern = @"\d+";
            //    MatchCollection matches = Regex.Matches(aobj.Code, pattern);
            //    string value = matches[matches.Count - 1].Value;
            //    int values = Convert.ToInt32(value);
            //    values++;
            //    m.Code = code + values.ToString("D4");
            //}
            //else
            //{
            //    int i = 1;
            //    m.Code = code + i.ToString("D4");
            //}
            message = "";
            var aobj = Context.TWarehouses.AsNoTracking().FirstOrDefault(x=>x.Code == m.Code);
            if (aobj != null)
            {
                message = "编码已存在！";
                return false;
            }
            m.CreateDate = DateTime.Now;
            Context.TWarehouses.Add(m);
            return Context.SaveChanges()>0;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelTWarehouse(int id)
        {
            try
            {
                var wh = Context.TWarehouses.Find(id);
                if (wh!=null)
                {
                    Context.TWarehouses.Remove(wh);
                    return Context.SaveChanges() > 0;
                }
                return false;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 列表显示
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<TWarehouse> GetAllTWarehouse(string? name, int pageIndex, int pageSize, out int totalCount)
        {
            try
            {
                var whList = Context.TWarehouses.AsQueryable();
                if (!string.IsNullOrEmpty(name))
                {
                    whList = whList.Where(c => c.Name.Contains(name));
                }
                totalCount = whList.Count();
                whList = whList.OrderByDescending(x => x.CreateDate).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                return whList.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TWarehouse GetTWarehouseById(int id)
        {
            try
            {
                return Context.TWarehouses.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改库房
        /// </summary>
        /// <param name="wh"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool UpdTWarehouse(TWarehouse wh,out string message)
        {
            try
            {
                message = "";
                var aobj = Context.TWarehouses.AsNoTracking().FirstOrDefault(x => x.Id != wh.Id && x.Code == wh.Code);
                if (aobj != null)
                {
                    message = "编码已存在！";
                    return false;
                }
                wh.ModifyDate = DateTime.Now;
                Context.TWarehouses.Update(wh);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
